package com.googlecode.mgwt.examples.uibinder.client.activities;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.examples.uibinder.client.BasicCell;
import com.googlecode.mgwt.examples.uibinder.client.Topic;
import com.googlecode.mgwt.ui.client.widget.CellList;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedEvent;

public class ExampleListDisplayGwtImpl extends Composite implements ExampleListDisplay {

	private static ExampleListDisplayGwtImplUiBinder uiBinder = GWT.create(ExampleListDisplayGwtImplUiBinder.class);

	@UiField(provided = true)
	CellList<Topic> list;

	private Presenter presenter;

	interface ExampleListDisplayGwtImplUiBinder extends UiBinder<Widget, ExampleListDisplayGwtImpl> {
	}

	public ExampleListDisplayGwtImpl() {
		list = new CellList<Topic>(new BasicCell<Topic>() {

			@Override
			public String getDisplayString(Topic model) {
				return model.getName();
			}

			@Override
			public boolean canBeSelected(Topic model) {
				return true;
			}
		});
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiHandler("list")
	protected void onCellSelected(CellSelectedEvent event) {
		if (presenter != null) {
			int index = event.getIndex();
			presenter.onItemSelected(index);
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;

	}

	@Override
	public void render(ArrayList<Topic> topicList) {
		list.render(topicList);
	}

	@Override
	public void setSelected(int lastIndex, boolean b) {
		list.setSelectedIndex(lastIndex, b);

	}

}
